package cn.xerllent.config;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.github.pagehelper.PageInterceptor;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.annotation.MapperScans;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.List;

@Configuration
@MapperScan("cn.xerllent.**.mapper")
//@MapperScans({@MapperScan("cn.xerllent.system.mapper"),@MapperScan("cn.xerllent.doc.mapper")})
public class MybatisPlusConfig {

//    @Bean("mybatisSqlSession")
//    public SqlSessionFactory sqlSessionFactory(DataSource dataSource, GlobalConfig globalConfig) throws Exception {
//        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
//        /* 数据源 */
//        sqlSessionFactory.setDataSource(dataSource);
//        /* 枚举扫描 */
//        //sqlSessionFactory.setTypeEnumsPackage("com.baomidou.mybatisplus.samples.mysql.enums");
//        /* xml扫描 */
//        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/**/*Mapper.xml"));
//        /* 扫描 typeHandler */
////        sqlSessionFactory.setTypeHandlersPackage("com.baomidou.mybatisplus.samples.mysql.type");
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        configuration.setJdbcTypeForNull(JdbcType.NULL);
//        /* 驼峰转下划线 */
//        configuration.setMapUnderscoreToCamelCase(true);
//        
//        //分页解析器1--- MybatisPlusInterceptor
//        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
//        sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
//        
//        //分页解析器2--- pagehelper的分页插件
//        PageInterceptor pagehelperInterceptor =new PageInterceptor();
//        sqlSessionFactory.setPlugins(pagehelperInterceptor);
//        
//        /* map 下划线转驼峰 */
//        configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
//        sqlSessionFactory.setConfiguration(configuration);
//        /* 自动填充插件 */
//        globalConfig.setMetaObjectHandler(new MysqlMetaObjectHandler());
//        sqlSessionFactory.setGlobalConfig(globalConfig);
//        return sqlSessionFactory.getObject();
//    }

   @Bean
   public GlobalConfig globalConfig() {
       GlobalConfig conf = new GlobalConfig();
       conf.setDbConfig(new GlobalConfig.DbConfig().setColumnFormat("`%s`"));
       DefaultSqlInjector logicSqlInjector = new DefaultSqlInjector() {
    	   
           /**
            * 注入自定义全局方法
            */
           @Override
           public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
               List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
               // 不要逻辑删除字段, 不要乐观锁字段, 不要填充策略是 UPDATE 的字段
               methodList.add(new InsertBatchSomeColumn(t -> !t.isLogicDelete() && !t.isVersion() && t.getFieldFill() != FieldFill.UPDATE));
               // 不要填充策略是 INSERT 的字段, 不要字段名是 column4 的字段
               methodList.add(new AlwaysUpdateSomeColumnById(t -> t.getFieldFill() != FieldFill.INSERT && !t.getProperty().equals("column4")));
               return methodList;
           }
       };
       conf.setSqlInjector(logicSqlInjector);
       return conf;
   }
   
   /**
    * pagehelper的分页插件
    */
   @Bean
   public PageInterceptor pageInterceptor() {
       return new PageInterceptor();
   }
   
   /**
    * MybatisPlus新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
    * mp 与 pagehelper 存在依赖 jsqlparser 冲突，不建议混用
    */
//   @Bean
//   public MybatisPlusInterceptor mybatisPlusInterceptor() {
//       MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//       interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.Mysql));
//       return interceptor;
//   }

//   @Bean
//   public ConfigurationCustomizer configurationCustomizer() {
//       return configuration -> configuration.setUseDeprecatedExecutor(false);
//   }
}
